package com.hmall.cart.listenrs;

import com.hmall.cart.service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class CreateOrderLister {
    private final ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue"),
            exchange = @Exchange(name = "trade.topic"),
            key = "order.create"
    ))
    public void listenCreateOrder(Map<String, Object> map){
        System.out.println("cartService收到消息");
        Collection<Long> itemIds = (Collection<Long>) map.get("itemIds");
        Integer userId = (Integer) map.get("userId");
        Long id = Long.valueOf(userId);
        System.out.println("userId:" + id);
        UserContext.setUser(id);
        cartService.removeByItemIds(itemIds);
    }
}
